package com.yiwei.zhh.adapter.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zhh.interfaces.OnRecyclerViewListener;

import java.util.List;

/**
 * Created by  on 16/3/21
 */
public class BaseViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {


    private SparseArray<View> views;////声明一个数据保存view，一般为android的sparry，类似于hashmap，并初始化

    //private BaseRViewAdapter.OnItemClickListener mOnItemClickListener ;////父类的点击事件
    List<T> datas;

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    public BaseViewHolder(View itemView, OnRecyclerViewListener onRecyclerViewListener, List<T> datas) {
        super(itemView);
        itemView.setOnClickListener(this);
        this.datas = datas;
        // this.mOnItemClickListener =onItemClickListener;
        this.onRecyclerViewListener = onRecyclerViewListener;
        this.views = new SparseArray<View>();////不清楚有几个view，用个集合存储view
    }

    public TextView getTextView(int viewId) {
        return retrieveView(viewId);
    }

    public CheckBox getCheckBox(int viewId) {
        return retrieveView(viewId);
    }

    public Button getButton(int viewId) {
        return retrieveView(viewId);
    }

    public ImageView getImageView(int viewId) {
        return retrieveView(viewId);
    }

    public View getView(int viewId) {
        return retrieveView(viewId);
    }

    //查找View：先从sparseArray中取，没有就从baseViewHolder中传入的view中找，然后放进sa中
    protected <T extends View> T retrieveView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }


    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public BaseViewHolder setText(int viewId, String text) {
        TextView view = retrieveView(viewId);
        view.setText(text);
        return this;
    }

    public SparseArray<View> getAllView() {
        return views;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public BaseViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = retrieveView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param bm
     * @return
     */
    public BaseViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = retrieveView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     * Glide加载图片==>设置页面可以进行清理缓存（fresco=》微视界，glide-》GanKMM）
     *
     * @param viewId
     * @param url
     * @return
     */
    public BaseViewHolder setImageByUrl(Context context, int viewId, String url) {
        //GlideUtils.loadImage(context, getImageView(viewId), url);
        return this;
    }

    /**
     * 为ImageView设置图片
     * Fresco加载图片==>设置页面可以进行清理缓存（fresco=》微视界，glide-》GanKMM(干货营)）
     *
     * @param viewId
     * @param url
     * @return
     */
    public BaseViewHolder setImageByFUrl(int viewId, String url) {
        //  FrecsoUtils.loadImage(url, (SimpleDraweeView) getImageView(viewId));
        return this;
    }

    @Override
    public void onClick(View v) {
        if (onRecyclerViewListener != null) {
            onRecyclerViewListener.onItemClick(datas,getLayoutPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (null != onRecyclerViewListener) {
            onRecyclerViewListener.onItemLongClick(datas, getLayoutPosition());
        }
        return false;
    }
}
